//
//  TestInfoModel.m
//  CloudTest
//
//  Created by 张帅 on 2020/7/22.
//  Copyright © 2020 张帅. All rights reserved.
//

#import "TestInfoModel.h"

@implementation TestInfoModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        _selected_song_list = [NSMutableArray new];
        _show_data = NO;
    }
    return self;
}
#pragma mark 根据考级码获取 考级信息 这个方法里面，使用串行队列吧第一次要展示的数据都请求下来了，返回一个完整的model
/// 根据考级码获取 考级信息 这个方法里面，使用串行队列吧第一次要展示的数据都请求下来了，返回一个完整的model
/// @param code 考级码
/// @param successBlock successBlock
/// @param errorBlock errorBlock
+ (void)get_info_code:(NSString *)code Success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock {
  __block TestInfoModel *model =[[TestInfoModel alloc] init];
    //创建串行队列
    dispatch_queue_t seial_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL);
    //创建信号量
    dispatch_semaphore_t semaphore_t  = dispatch_semaphore_create(0);
    
   __block BOOL success = YES;
    
    dispatch_async(seial_queue, ^{
         [TestInfoModel get_ExamCode:code Success:^(id obj) {//根据考级码获取k科目信息
             model.project_info_model=obj;
             model.projectList = model.project_info_model.subjectList;
             model.currentProject = model.projectList.firstObject;
             model.code=code;
            dispatch_semaphore_signal(semaphore_t);
          } error:^{
              success =NO;
               dispatch_semaphore_signal(semaphore_t);
              
          }];
        dispatch_semaphore_wait(semaphore_t, DISPATCH_TIME_FOREVER);//不能再主线程中使用
    });
    
    
    dispatch_async(seial_queue, ^{
        if (success==NO) {
            dispatch_semaphore_signal(semaphore_t);
            return;
        }
        [TestInfoModel getLevelProjectId:model.currentProject.Id examCode:model.code Success:^(NSArray *array) {//获取等级列表
            model.level_list=array;
            model.currentLevelModel=model.level_list.firstObject;
           dispatch_semaphore_signal(semaphore_t);

        } error:^{
            success =NO;
            dispatch_semaphore_signal(semaphore_t);
        }];
        dispatch_semaphore_wait(semaphore_t, DISPATCH_TIME_FOREVER);//不能再主线程中使用
    });

     dispatch_async(seial_queue, ^{
         dispatch_async(dispatch_get_main_queue(), ^{
             if (success) {
                 if (successBlock) {
                     successBlock(model);
                 }
             } else {
                 if (errorBlock) {
                     errorBlock();
                 }
             }
         });
     });
}
#pragma mark 根据考级码获取k科目信息
/// 根据考级码获取k科目信息
/// @param code 考级码
/// @param successBlock successBlock
/// @param errorBlock errorBlock
+ (void)get_ExamCode:(NSString *)code Success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock {
    NSString * url = [NSString stringWithFormat:@"%@referralCode=%@",get_ExamCode_Url,code];
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
           NSLog(@"通过考级码获取科目信息 %@",responseObject);
           if ([AppTools isStateSuccess:responseObject]) {
               ProjectListModel *model = [ProjectListModel yy_modelWithJSON:responseObject[@"data"]];
               if (successBlock) {
                   successBlock(model);
               }
           } else {
               if (errorBlock) {
                   errorBlock();
               }
           }
       } failure:^(NSError *error) {
           NSLog(@"hou error %@",error);
           if (errorBlock) {
               errorBlock();
           }
           NSString *errorStr = [self errorDescribe:error];
           [QMUITips showError:errorStr];
       }];
}
#pragma mark 根据科目id 获取等级列表
/// 根据科目id 获取等级列表
/// @param projectId 科目id
/// @param examCode 考级码
/// @param successBlock successBlock
/// @param errorBlock errorBlock
+(void)getLevelProjectId:(NSString *)projectId examCode:(NSString *)examCode Success:(ArraySuccess)successBlock error:(ErrorBlock)errorBlock {
    NSString * url = [NSString stringWithFormat:@"%@referralCode=%@&subjectId=%@",get_Level_BySubjectId_Url,examCode,projectId];
    NSLog(@" 根据科目id 获取等级列表== %@",url);
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
              NSLog(@"根据科目id 获取等级列表 %@",responseObject);
              if ([AppTools isStateSuccess:responseObject]) {
                  NSArray * array = [NSArray yy_modelArrayWithClass:[LevelModel class] json:responseObject[@"data"]];
                  if (successBlock) {
                      successBlock(array);
                  }
              } else {
                  if (errorBlock) {
                      errorBlock();
                  }
              }
          } failure:^(NSError *error) {
              NSLog(@"根据科目id 获取等级列表 error %@",error);
              if (errorBlock) {
                  errorBlock();
              }
              NSString *errorStr = [self errorDescribe:error];
              [QMUITips showError:errorStr];
          }];
}
#pragma mark 切换项目
- (void)switch_project_success:(ArraySuccess)successBlock {
    [TestInfoModel getLevelProjectId:self.currentProject.Id examCode:self.code Success:^(NSArray *array) {//获取等级列表
       self.level_list=array;
       self.currentLevelModel=self.level_list.firstObject;
        if (successBlock) {
             successBlock(nil);
         }
    } error:^{
        
    }];
}

#pragma mark 上传证书
-(void)upload_certificate_image_Success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock {
    [NetWorkTools uploadImageWithImage:self.currentLevelModel.certificate_image success:^(NSString *urls) {
        NSLog(@"urls = %@",urls);
        self.currentLevelModel.certificate_image_url = urls;
        if (successBlock) {
            successBlock(nil);
        }
    } failure:^(NSError *error) {
        NSLog(@"urls error");
        if (errorBlock) {
            errorBlock();
        }
    } progress:^(float progress) {
        NSLog(@"progress = %f",progress);
    }];
}


//获取选择的项目名数组
- (NSMutableArray *)get_select_projectName_list {
    NSMutableArray * projectName_list = [NSMutableArray new];
    for (ProjectModel *model in self.projectList) {
        [projectName_list addObject:model.value];
    }
    return projectName_list;
}
//获取选择的等级名数组
- (NSMutableArray *)get_select_levelName_list {
    NSMutableArray * level_name_list = [NSMutableArray new];
    for (LevelModel *model in self.level_list) {
        [level_name_list addObject:model.name];
    }
    return level_name_list;
}





@end



@implementation ProjectListModel

// 声明自定义类参数类型
+ (NSDictionary *)modelContainerPropertyGenericClass {
  return @{@"subjectList" : [ProjectModel class]};
}

@end

@implementation ProjectModel
//字段名映射
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"Id" : @"id"};
}

@end

#pragma mark 等级
@implementation LevelModel
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}
//是否需要选择证书
- (BOOL)need_certificate_image {
    int level = [self.level intValue];
    if (level > 1) {
        return YES;
    }
    return NO;
}
//字段名映射
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"Id" : @"id"};
}
// 声明自定义类参数类型
+ (NSDictionary *)modelContainerPropertyGenericClass {
  return @{@"groupVos" : [GroupVos class]};
}
//默认状态
- (void)default_state {
    for (GroupVos *model in self.groupVos) {
        model.song_name = @"";
        model.selected = NO;
        [model.tuneList makeObjectsPerformSelector:@selector(setIsSelected:) withObject:@(NO)];//该分组下所有曲目设置为no
    }
}

@end



#pragma mark 曲目分组
@implementation GroupVos
- (instancetype)init {
    self = [super init];
    if (self) {
        _selected = NO;
    }
    return self;
}
// 声明自定义类参数类型
+ (NSDictionary *)modelContainerPropertyGenericClass {
   return @{@"tuneList" : [SongModel class]};
}


//获取选择的曲目数组
- (NSMutableArray *)get_select_songName_list {
    NSMutableArray * level_name_list = [NSMutableArray new];
    for (SongModel *model in self.tuneList) {
        [level_name_list addObject:model.name];
    }
    return level_name_list;
}

//设置选中曲目
- (void)set_select_songMode_songName:(NSString *)songName {
    [self.tuneList makeObjectsPerformSelector:@selector(setIsSelected:) withObject:@(NO)];
    for (SongModel *model in self.tuneList) {
        if ([model.name isEqualToString:songName]) {
            model.isSelected = YES;
            break;
        }
    }
}
@end



#pragma mark 曲目model
@implementation SongModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        _isSelected = NO;
    }
    return self;
}
//字段名映射
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"Id" : @"id"};
}
/// 获取曲目列表
/// @param orderId 订单id
/// @param successBlock successBlock
/// @param errorBlock errorBlock
+ (void)get_song_list_orderId:(NSString *)orderId success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock  {
    NSString * url = [NSString stringWithFormat:@"%@orderId=%@",get_song_list_url,orderId];
      [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
          NSLog(@"获取曲目列表 = %@",responseObject);
          if ([AppTools isStateSuccess:responseObject]) {
              NSArray * array =[NSArray yy_modelArrayWithClass:[SongModel class] json:responseObject[@"data"]];
              if (successBlock) {
                  successBlock(array);
              }
          } else {
              if (errorBlock) {
                  errorBlock();
              }
          }
      } failure:^(NSError *error) {
          if (errorBlock) {
              errorBlock();
          }
          NSLog(@"获取曲目列表error = %@",error);
          NSString *errorStr = [self errorDescribe:error];
          [QMUITips showError:errorStr];
      }];
}

+ (void)get_order_details_model_orderId:(NSString *)orderId success:(ObjSuccess)successBlock error:(ErrorBlock)errorBlock {
   
}
@end
